# Author : marcg
# Version 1
# Date : 2 november 2008
import os
from google.appengine.ext.webapp import template
import cgi
#from model import Product
from product import Product
from owner import Owner, NotFound
from wish import Wish
import sys

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db


class MyList(webapp.RequestHandler):
  def get(self):
    loggedInuser = users.get_current_user()

    if loggedInuser:
	wishes_query = db.GqlQuery("SELECT * FROM Wish where email = :1", loggedInuser.email())
	wishes = wishes_query.fetch(1000)
        
	try:
		owner = Owner.find(loggedInuser.email())
		sys.stderr.write('found existing user\n')
		url = users.create_logout_url(self.request.uri)
		url_linktext = 'Logout'

		template_values = {
		  'owner': owner,
		  'wishes': wishes,
		  'url': url,
		  'url_linktext': url_linktext,
		  }
		    
		path = os.path.join(os.path.dirname(__file__), 'mylist.html')
	        self.response.out.write(template.render(path, template_values))
	except NotFound:
 		sys.stderr.write('user not found, adding it\n')
 		self.redirect('/owner/register')

    else:
      self.redirect(users.create_login_url(self.request.uri))


class YourList(webapp.RequestHandler):
  def post(self):
                   
        self.get()
    
  def get(self):
                   
        wishes_query = db.GqlQuery("SELECT * FROM Wish where email = :1", self.request.get('email'))
        wishes = wishes_query.fetch(1000)
 
 
 	buy_urls = []
 	for item in wishes_query:
  		buy_urls.append('<a href="/edit?id=%d">Edit</a> - ' %  
  		item.key().id())

		
        template_values = {
          'buy_urls' : buy_urls,
          'wishes': wishes,
          }
    
        path = os.path.join(os.path.dirname(__file__), 'yourlist.html')
        self.response.out.write(template.render(path, template_values))
    
    


class Add(webapp.RequestHandler):
    def get(self):
    	
	
	wish = Wish()
	wish.user = users.get_current_user()
	wish.email= users.get_current_user().email()

	product = Product.get(db.Key.from_path(Product.kind(), int(self.request.get('id'))))	

	if product:
		wish.product = product 
		wish.put()
		#vreselijk, die betekenisvolle indent. What's wrong with curly braces?!
	self.redirect('/wish/mylist')
    
    def post(self):
	    wish = Wish()
	    wish.user = users.get_current_user()
	    wish.email= users.get_current_user().email()
	    
	    if id:
	    	product = Product.get(db.Key.from_path(Wish.kind(), int(self.request.get('id'))))	
	    else:
	    	product_query = db.GqlQuery("select * from Product where name = :name", name=self.request.get('name'))
	    	product = product_query.fetch(1)
	    
	    if product:
	    	wish.product = product[0] 
	      	wish.put()
	    #vreselijk, die betekenisvolle indent. What's wrong with curly braces?!
	    self.redirect('/wish/mylist')
	

class Buy(webapp.RequestHandler):
	def get(self):
	#get product
          wish = Wish.get(db.Key.from_path(Wish.kind(), int(self.request.get('id'))))

          template_values = {
                'wish': wish
                  }
          #show them
                          
          path = os.path.join(os.path.dirname(__file__), 'buy.html')
          self.response.out.write(template.render(path, template_values))

	def post(self):
		wish = Wish.get(db.Key.from_path(Wish.kind(), int(self.request.get('id'))))
		wish.buyer = self.request.get('e-mail')
		wish.put()
		self.redirect('/wish/yourlist')


application = webapp.WSGIApplication(
                                     [('/wish/add', Add),
                                      ('/wish/mylist',MyList),
                                      ('/wish/yourlist',YourList),
                                      ('/wish/yourlist/buy', Buy)
                                      ],
                                     debug=True)
		

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
